<?php
/**
 * Created by PhpStorm.
 * User: Mac
 * Date: 2019-01-13
 * Time: 11:13
 */
//服务优化过程到类
class http{
    CONST HOST='0.0.0.0';
    CONST PORT=9504;
    public $http=null;

    public function __construct()
    {
        $this->http = new swoole_http_server("0.0.0.0", 9504);


        //设置显示静态文件
        $this->http->set(
            [
                'enable_static_handler'=>true,
                'document_root'=>'/home/wwwroot/default/tp5/public/static/',
                'worker_num'=>4,
                'task_worker_num'=>2

            ]
        );
        $this->http->on('WorkerStart',[$this,'onWorkerStart']);
        $this->http->on('request',[$this,'onRequest']);
        $this->http->on('task',[$this,'onTask']);//必须
        $this->http->on('finish',[$this,'onFinish']);//必须有
        $this->http->on('close',[$this,'onClose']);//必须有

        $this->http->start();
    }

    /**
     * @param $ser
     * @param $worder_id
     */
    public function onWorkerStart($ser,$worder_id){
        //定义应用常量
        define('APP_PATH', __DIR__ . '/../application/');
        // ThinkPHP 引导文件
        // 加载基础文件
        require __DIR__ . '/../thinkphp/base.php';
    }
    /**
     * @param $request
     * @param $response
     */
    public function onRequest($request,$response){
        $_SERVER=[];
        if(isset($request->server)){
            foreach ($request->server as $k=>$v){
                $_SERVER[strtoupper($k)]=$v;
            }
        }
        if(isset($request->header)){
            foreach ($request->header as $k=>$v){
                $_SERVER[strtoupper($k)]=$v;
            }
        }
        $_GET=[];
        if(isset($request->get)){
            foreach ($request->get as $k=>$v){
                $_GET[$k]=$v;
            }
        }
        if(empty($_GET)){
            unset($_GET);
        }
        $_POST=[];
        if(isset($request->post)){
            foreach ($request->post as $k=>$v){
                $_POST[$k]=$v;
            }
        }
        ob_start();
        $_POST['http_server']=$this->http;
// 执行应用并响应
        try{
            think\Container::get('app', [defined('APP_PATH') ? APP_PATH : ''])
                ->run()
                ->send();
        }catch (\Exception $e){

        }

        $res=ob_get_contents();
        ob_clean();
        echo request()->action().PHP_EOL;
        $response->cookie('wjp','wujiangpan',time()+3600);
        $response->end($res);
        //$server->close();//暴力关闭
    }
    /**
     * @param $serv
     * @param $taskId
     * @param $workId
     * @param $data
     * @return string
     */
    public function onTask($serv,$taskId,$workId,$data){
        print_r($data);
        //这里可以处理阿里大于的发送功能
        $task=new \app\Common\lib\task\Task();

        //耗时10s
        sleep(10);
        return "on task finish";//告诉woker

    }

    public function onFinish($serv,$taskId,$data){
        echo "taskid:{$taskId}\n";
        echo "finish-data-success:{$data}\n";
    }

    /**
     * @param $ws
     * @param $frame
     */
    public function onClose($ws,$fd){
        echo "close".$fd.'/n';

    }
}

$http=new http();